import { ref, type Ref } from 'vue'

import { useRouter,useRoute } from 'vue-router'
import type{IAddressemit} from '../types/type'
import { useAddressList } from '../stores/addressList'


export const useAddress = () => {
  //返回上一级
  const route=useRoute()
  const onClickLeft = () =>history.back()
    const chosenAddressId = ref('1')
    const addressList = useAddressList()
    const router = useRouter()
   
  //新增地址
    const onAdd = () => {
      router.push('/addressEmit')
      addressList.init()
    }
  //编辑地址
    const onEdit = (item:IAddressemit) => {
      // console.log(item);
      
        const {name,id,isDefault,postalCode,tel,address,areaCode,addressDetail}=item
        // console.log('item', item)
        const disabledList: any={
          id,
          name, 
          isDefault,
          postalCode,
          tel,
          address,
          areaCode,
          addressDetail
        }
        addressList.useAddressEmit(disabledList)
        router.push('/addressEmit')
    }
    
    return { chosenAddressId, onAdd, onEdit, addressList, onClickLeft }
}
